﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace SmartHouseOOP
{
    public class XmlProcessor
    {
        private XmlDocument doc;
        private string sourceFile;
        public XmlProcessor(string url)
        {
            doc = new XmlDocument();
            this.sourceFile = url;

        }
        public List<Room> ReadXmlFile()
        {
            doc.Load(sourceFile);
            List<Room> rooms = new List<Room>();
            foreach (XmlNode node in doc.DocumentElement)
            {
                Room currentRoom;
                string nameOfRoom = node.Attributes[0].Value;
                int number = Int32.Parse(node["number"].InnerText);
                currentRoom = new Room(number, nameOfRoom);
                for (int i = 0; i < node.SelectNodes("device").Count; i++)
                {
                    AddOneDevice(node, currentRoom, i);
                }
                rooms.Add(currentRoom);
            }
            return rooms;
        }
        public void AddOneDevice(XmlNode node,Room inputRoom,int i)
        {
            string deviceName = node.SelectNodes("device").Item(i).InnerText;
            string label = node.SelectNodes("device").Item(i).Attributes["type"].Value;
            Device newDevice = new Device(deviceName, label);
            inputRoom.AddOneDevice(newDevice);
        }
    }
}

